{layout name="layout" /}

<div class="layui-form" id="menu_add"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">
            <input type="text" name="avatar_src" placeholder="链接" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="root_name" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">登陆密码</label>
        <div class="layui-input-block">
            <input type="text" name="root_password" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱地址</label>
        <div class="layui-input-block">
            <input type="text" name="email" placeholder="联系邮箱" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">联系手机</label>
        <div class="layui-input-block">
            <input type="text" name="mobile" placeholder="手机号码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户组</label>
        <div class="layui-input-block">
            <select name="group_id" lay-filter="aihao">
                {foreach $allRootGroupList as $key=>$vo }
                <option value="{$vo.id|raw}">{$vo.root_group_name}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" checked lay-skin="switch">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->
</div>
<script>
    layui.use('form', function(){
        var form = layui.form;

        //各种基于事件的操作，下面会有进一步介绍
        form.on('submit(*)', function(data){
            // console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            // console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            if(data.field['status']){
                data.field['status'] = 1;
            }else{
                data.field['status'] = 0;
            }

            if(!data.field['avatar_src']){
                data.field['avatar_src'] = '无';
            }

            if(!data.field['root_name']){
                layer.msg('请输入用户名');
                return false;
            }

            if(!data.field['root_password']){
                layer.msg('请输入登陆密码');
                return false;
            }

            data.field['group_id'] = parseInt(data.field['group_id']);


            var addArray = data.field;

            console.log(addArray) //当前容器的全部表单字段，名值对形式：{name: value}

            $.ajax({
                url: "/admin/root/add",
                type: "POST",
                data:{
                    'addArray':addArray,
                },
                dataType: "json",
                success: function(result){
                    console.log(result)
                    if(result.code === 0){
                        layer.confirm(result.msg+',请选择下一步操作', {
                            btn: ['继续添加','返回列表页'] //按钮
                        }, function(){
                            location.reload();
                        }, function(){
                            window.location='/admin/root/index'
                        });
                    }else{
                        layer.msg(result.msg)
                    }
                },
                error:function(err){
                    layer.msg(result.msg)
                    console.log(err.statusText);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        })
    });
</script>